package com.osmino.contacts.entity;

import android.database.Cursor;
import android.provider.ContactsContract;

public final class StructuredName implements Comparable<StructuredName> {

	public static final String ID_COLUMN = ContactsContract.CommonDataKinds.StructuredName._ID;
	public static final String DISPLAY_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME;
	public static final String GIVEN_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME;
	public static final String FAMILY_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME;
	public static final String PREFIX_COLUMN = ContactsContract.CommonDataKinds.StructuredName.PREFIX;
	public static final String MIDDLE_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME;
	public static final String SUFFIX_COLUMN = ContactsContract.CommonDataKinds.StructuredName.SUFFIX;
	public static final String PHONETIC_GIVEN_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.PHONETIC_GIVEN_NAME;
	public static final String PHONETIC_MIDDLE_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.PHONETIC_MIDDLE_NAME;
	public static final String PHONETIC_FAMILY_NAME_COLUMN = ContactsContract.CommonDataKinds.StructuredName.PHONETIC_FAMILY_NAME;
	public final long id;
	public final String displayName;
	public final String givenName;
	public final String familyName;
	public final String prefix;
	public final String middleName;
	public final String suffix;
	public final String phoneticGivenName;
	public final String phoneticMiddleName;
	public final String phoneticFamilyName;

	public StructuredName(long id, String displayName, String givenName, String familyName,
			String prefix, String middleName, String suffix, String phoneticGivenName,
			String phoneticMiddleName, String phoneticFamilyName) {
		this.id = id;
		this.displayName = displayName;
		this.givenName = givenName;
		this.familyName = familyName;
		this.prefix = prefix;
		this.middleName = middleName;
		this.suffix = suffix;
		this.phoneticGivenName = phoneticGivenName;
		this.phoneticMiddleName = phoneticMiddleName;
		this.phoneticFamilyName = phoneticFamilyName;
	}
	
	public StructuredName(Cursor cursor) {
		id = cursor.getLong(cursor.getColumnIndex(ID_COLUMN));
		displayName = resolveName(cursor, DISPLAY_NAME_COLUMN);
		givenName = resolveName(cursor, GIVEN_NAME_COLUMN);
		familyName = resolveName(cursor, FAMILY_NAME_COLUMN);
		prefix = resolveName(cursor, PREFIX_COLUMN);
		middleName = resolveName(cursor, MIDDLE_NAME_COLUMN);
		suffix = resolveName(cursor, SUFFIX_COLUMN);
		phoneticGivenName = resolveName(cursor, PHONETIC_GIVEN_NAME_COLUMN);
		phoneticMiddleName = resolveName(cursor, PHONETIC_MIDDLE_NAME_COLUMN);
		phoneticFamilyName = resolveName(cursor, PHONETIC_FAMILY_NAME_COLUMN);
	}
	
	private static String resolveName(Cursor cursor, String column) {
		String result = "";
		int columnIndex = cursor.getColumnIndex(column);
		if (columnIndex != -1) {
			result = cursor.getString(columnIndex);
		}
		return result;
	}

	@Override
	public int compareTo(StructuredName another) {
		int result = Long.valueOf(id).compareTo(another.id);
		if (result == 0) {
			result = displayName.compareTo(another.displayName);
		}
		if (result == 0) {
			result = givenName.compareTo(another.givenName);
		}
		if (result == 0) {
			result = familyName.compareTo(another.familyName);
		}
		if (result == 0) {
			result = prefix.compareTo(another.prefix);
		}
		if (result == 0) {
			result = middleName.compareTo(another.middleName);
		}
		if (result == 0) {
			result = suffix.compareTo(another.suffix);
		}
		if (result == 0) {
			result = phoneticGivenName.compareTo(another.phoneticGivenName);
		}
		if (result == 0) {
			result = phoneticMiddleName.compareTo(another.phoneticMiddleName);
		}
		if (result == 0) {
			result = phoneticFamilyName.compareTo(another.phoneticFamilyName);
		}
		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof StructuredName)) {
			return false;
		}
		StructuredName rhs = (StructuredName) o;
		return compareTo(rhs) == 0;
	}

	@Override
	public int hashCode() {
		int result = 17;
		result = 31 * result + (int) (id ^ (id >>> 32));
		result = 31 * result + displayName.hashCode();
		result = 31 * result + givenName.hashCode();
		result = 31 * result + familyName.hashCode();
		result = 31 * result + prefix.hashCode();
		result = 31 * result + middleName.hashCode();
		result = 31 * result + suffix.hashCode();
		result = 31 * result + phoneticGivenName.hashCode();
		result = 31 * result + phoneticMiddleName.hashCode();
		result = 31 * result + phoneticFamilyName.hashCode();
		return result;
	}
}
